package com.whub507.msgmanager.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.whub507.msgmanager.common.vo.AddGroupVO;
import com.whub507.msgmanager.common.vo.GroupVO;
import com.whub507.msgmanager.common.vo.UserVO;
import com.whub507.msgmanager.domain.Group;
import com.whub507.msgmanager.mapper.GroupMapper;
import com.whub507.msgmanager.mapper.UserMapper;
import com.whub507.msgmanager.service.GroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class GroupServiceImpl extends ServiceImpl<GroupMapper, Group> implements GroupService{

    @Resource
    private GroupMapper groupMapper;

    @Resource
    private UserMapper userMapper;

    @Override
    public List<GroupVO> getGroup(Integer userId) {
        List<GroupVO> allGroup = groupMapper.selectAllGroupByUserId(userId);

        return allGroup;
    }

    @Override
//    public Boolean addGroup(Integer userId, String groupName,List<String> nameAndPhone) {
    public Boolean addGroup(AddGroupVO addGroupVO){
        try {
            List<Group> groupList = groupMapper.selectAllByUserIdAndGroupName(addGroupVO.getUserId(), addGroupVO.getGroupName());

            if (groupList.size() > 0)
                return Boolean.FALSE;

            addGroupVO.getNameAndPhone().stream().forEach(o -> {
                String name = o.split("-")[0];
                String phone = o.split("-")[1];

                groupMapper.insertNewGroup(new Group(addGroupVO.getUserId(), name, phone, addGroupVO.getGroupName()));
            });
        } catch (Exception e){
            return Boolean.FALSE;
        }
        return Boolean.TRUE;

    }

    @Override
    public List<UserVO> getUserStructure() {
        List<UserVO> res = userMapper.selectUserStructure();
        return res;
    }
    
}
